#include "dialoggoto.h"
#include "ui_dialoggoto.h"

DialogGoto::DialogGoto(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogGoto)
{
    ui->setupUi(this);

    QRegExp regx("[A-Fa-f0-9]+");
    QValidator *validator = new QRegExpValidator(regx, ui->lineEdit);
    ui->lineEdit->setValidator(validator);


    connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onTypeIndexChange(int)));
}

void DialogGoto::onTypeIndexChange(int index)
{
    ui->lineEdit->setText("");

    if (index == 0)
    {
        QRegExp regx("[A-Fa-f0-9]+");
        QValidator *validator = new QRegExpValidator(regx, ui->lineEdit);
        ui->lineEdit->setValidator(validator);
    }
    else if (index == 1)
    {
        QRegExp regx("[0-9]+");
        QValidator *validator = new QRegExpValidator(regx, ui->lineEdit);
        ui->lineEdit->setValidator(validator);
    }
    else if (index == 2)
    {
        QRegExp regx("[0-7]+");
        QValidator *validator = new QRegExpValidator(regx, ui->lineEdit);
        ui->lineEdit->setValidator(validator);
    }
}

long long DialogGoto::address()
{
    int type = ui->comboBox->currentIndex();
    QString addr = ui->lineEdit->text();
    bool ok = false;
    long long a = -1;

    if (addr.isEmpty()) \
    {
        return -1;
    }

    if (type == 0)
    {
        a = addr.toLongLong(&ok, 16);
    }
    else if (type == 1)
    {
        a = addr.toLongLong(&ok, 10);
    }
    else if (type == 2)
    {
        a = addr.toLongLong(&ok, 8);
    }

    if (!ok)
    {
        return -1;
    }

    return a;
}

DialogGoto::~DialogGoto()
{
    delete ui;
}
